<template>
    <div class="container">
        <div class="login">
            <div class="logo">
                <img src="../../assets/下载.png" alt="" />
            </div>
            <form action="">
                <div class="name item">
                    <div class="left">
                        <label for=""><span>*</span>用户名:</label>
                    </div>
                    <div class="right">
                        <div class="box">
                            <input type="text" v-model="account" />
                        </div>
                    </div>
                </div>
                <div class="psw item">
                    <div class="left">
                        <label for="">密码:</label>
                    </div>
                    <div class="right">
                        <div class="box">
                            <input type="password" v-model="password" />
                        </div>
                    </div>
                </div>
                <div class="radio">
                    <el-radio v-model="identity" label="Sidebar"
                        >管理员</el-radio
                    >
                    <el-radio v-model="identity" label="Sidebar1"
                        >销售员</el-radio
                    >
                </div>
                <div class="submit">
                    <el-button type="success" @click="add">登录</el-button>
                </div>
            </form>
        </div>
    </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
//引入辅助函数
const { mapState, mapActions } = createNamespacedHelpers("logins");
export default {
    data() {
        return {
            identity: "Sidebar",
            account: "",
            password: "",
        };
    },
    methods: {
        ...mapActions(["login"]),

        async add() {
            if (this.account && this.password) {
                let data = await this.login({
                    identity: this.identity,
                    account: this.account,
                    password: this.password,
                });
                if (data.status && data.data[0].status) {
                    localStorage.manager = this.identity;
                    if (this.identity == "Sidebar") {
                        localStorage.setItem(
                            "user",
                            JSON.stringify({
                                _id: data.data[0]._id,
                                account: data.data[0].account,
                            })
                        );
                    } else {
                        localStorage.setItem(
                            "user",
                            JSON.stringify({
                                _id: data.data[0]._id,
                                account: data.data[0].salesName,
                            })
                        );
                    }
                    console.log(data);
                    this.loginSuccess();
                    this.$router.push({
                        name: "insuranceSystem",
                        params:{identity:this.identity}
                    });
                } else {
                    this.loginFail()
                }
            } else {
                alert("请输入账号或密码 . ");
            }
        },
        loginSuccess() {
            this.$notify({
                message: "登陆成功",
                type: "success",
            });
        },
        loginFail() {
            this.$notify.error({
                message: "账号或密码错误",
            });
        },
    },
};
</script>

<style lang="scss">
body {
    background-color: #eceff4;
}

body .container {
    width: 1000px;
    background: url(../../assets/bg.jpg) center center no-repeat;
    background-size: cover;
    height: 360px;
    left: 50%;
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
}

body .container .login {
    box-shadow: 2px 2px 10px 3px rgb(185, 180, 180);
    width: 360px;
    // height: 360px;
    background-color: #f0f2f7;
    padding: 20px 40px;
    float: right;
}

body .container .login .logo {
    text-align: center;
}

body .container .login .logo img {
    width: 150px;
}

body .container .login form {
    width: 100%;
    margin-top: 30px;
}

body .container .login form .item {
    width: 280px;
    height: 32px;
    margin-bottom: 24px;
    display: flex;
}

body .container .login form .item .left {
    width: 70px;
    height: 32px;
    text-align: right;
    font-size: 14px;
    line-height: 32px;
}

body .container .login form .item .left span {
    color: red;
}

body .container .login form .item .right {
    flex: 1;
    line-height: 32px;
}

body .container .login form .item .right .box {
    background: white;
    width: 212px;
    height: 34px;
    border: 1px solid #d9d9d9;
    border-radius: 5px;
    line-height: 34px;
    display: flex;
    align-items: center;
    overflow: hidden;
    transition: 0.3s;
}

body .container .login form .item .right .box:hover {
    border-color: #6fa274;
}

body .container .login form .item .right .box .fa {
    color: #949494;
    margin: 0 10px;
    cursor: pointer;
}

body .container .login form .item .right .box input {
    border: none;
    outline: none;
    padding-left: 10px;
    flex: 1;
    font-size: 14px;
}

body .container .login form .item .pad {
    padding-left: 30px;
    display: flex;
    align-items: center;
    font-size: 13px;
}

body .container .login form .item .pad button {
    width: 80px;
    height: 33px;
    line-height: 33px;
    text-align: center;
    border: none;
    cursor: pointer;
    border-radius: 5px;
    color: #fff;
    background: #6da372;
    border-color: #6da372;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.12);
    box-shadow: 0 2px 0 rgba(0, 0, 0, 0.045);
    transition: 0.5s;
}

body .container .login form .item .pad button:hover {
    opacity: 0.8;
}

body .container .login form .item .pad input {
    width: 14px;
    height: 14px;
    border: 1px solid #6fa274;
    margin-right: 10px;
}

body .container .login form .item .pad input:hover {
    border-color: #6fa274;
}
body .container .login form .item .pad #goregister {
    margin-left: 50px;
    font-size: 12px;
    color: #6fa274;
}
form .submit {
    text-align: center;
}
form .submit a {
    text-decoration: none;
    margin-left: 20px;
}
.container .login .radio {
    text-align: center;
    margin-bottom: 20px;
}
</style>>